<?php
require_once('functions/connectDB.php');


// $query = "select * from groups";
// $results = mysql_query($query) or die(mysql_error());
// if($results)
// {
	// echo '<table border="1">';
	// echo '<thead>';
	// echo '<tr><th>ID</th><th>Name</th>';
	// echo '</thead>';
	// echo '<tbody>';
	// while ($row = mysql_fetch_array($results)) {
		// extract($row);
		// echo '<tr><td>' .$idgroups . '</td><td>' . $groupName . '</td></tr>';
	// }
	// echo '</tbody>';
	// echo '</table>';
	
//}
$query1 = "select * from groups";
echo "<table border = '1' align = 'center'>
			<thead><tr><th>Group ID</th><th>Group Name</th><th>Members</th></tr></thead><tbody>";
if($result = mysql_query($query1, $db)) {

	while($row = mysql_fetch_array($result)) {

		echo "<tr><td>".$row['idgroups']."</td><td>".$row['groupName']."</td>" ;
		$query2 = "select name from students where idgroups = {$row['idgroups']}";
		$result2 = mysql_query($query2, $db);
		$strings ="";
		while ($row2 = mysql_fetch_array($result2))
		{
			$strings .= $row2['name'].',';
		}
		$membersName = substr($strings,0,-1);
		print "<td>{$membersName}</td></tr>";
		
	}

	echo "</tbody></table>";

}
	else {
		print "<p>Could not retrieve the data because:<br/>".mysql_error($db).".</p>
		<p>The query being run was: ".$query. "</p>";
	}

mysql_close($db);

?>